package com.lesso.lpq.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lpq.sunflower.domain.repository.AttendanceRuleRepository;
import com.lesso.lpq.sunflower.infra.assembler.AttendanceRuleAssembler;
import com.lesso.lpq.sunflower.infra.mapper.AttendanceRuleMapper;
import com.lesso.lpq.sunflower.model.AttendanceRule;
import com.lesso.lpq.sunflower.pojo.dto.AttendanceRuleDTO;
import com.lesso.lpq.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import com.lesso.lpq.sunflower.pojo.vo.AttendanceRuleVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName : AttendanceRuleRepositoryImpl
 * @Description :
 * @Author : 罗培棋
 * @Date: 2025-07-05 17:15
 */
@Repository
public class AttendanceRuleRepositoryImpl implements AttendanceRuleRepository {
    private final AttendanceRuleMapper attendanceRuleMapper;

    public AttendanceRuleRepositoryImpl(AttendanceRuleMapper attendanceRuleMapper) {
        this.attendanceRuleMapper = attendanceRuleMapper;
    }

    @Override
    public Long save(AttendanceRule attendanceRule) {
        if (attendanceRule.getId() == null) {
            attendanceRuleMapper.insert(attendanceRule);
        } else {
            attendanceRuleMapper.updateById(attendanceRule);
        }
        return attendanceRule.getId();
    }

    @Override
    public Page<AttendanceRuleVO> getPageList(AttendanceRuleQueryDTO dto) {
        Page<AttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<AttendanceRule> wrapper = new EntityWrapper<>();
        wrapper.eq("del_status", 0);

        if (!StringUtils.isEmpty(dto.getRuleName())) {
            wrapper.like("rule_name", dto.getRuleName());
        }
        if (!StringUtils.isEmpty(dto.getRuleCode())) {
            wrapper.eq("rule_code", dto.getRuleCode());
        }

        List<AttendanceRule> attendanceRules = attendanceRuleMapper.selectPage(page, wrapper);
        List<AttendanceRuleVO> attendanceRuleVOs = AttendanceRuleAssembler.INSTANCE.entityListToVOList(attendanceRules);
        page.setRecords(attendanceRuleVOs);
        return page;
    }

    @Override
    public Page<AttendanceRuleVO> getCustomPageList(AttendanceRuleQueryDTO dto) {
        Page<AttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        RowBounds rowBounds = new RowBounds((dto.getCurrent() - 1) * dto.getSize(), dto.getSize());
        List<AttendanceRuleVO> attendanceRuleVOs = attendanceRuleMapper.selectPageList(rowBounds, dto);
        page.setRecords(attendanceRuleVOs);
        return page;
    }

    @Override
    public void removeById(Long id) {
        attendanceRuleMapper.deleteById(id);
    }

    @Override
    public void updateById(AttendanceRuleDTO dto) {
        AttendanceRule attendanceRule = AttendanceRuleAssembler.INSTANCE.dtoToEntity(dto);
        attendanceRuleMapper.updateById(attendanceRule);
    }

    @Override
    public AttendanceRuleVO getByRuleCode(String ruleCode) {
        EntityWrapper<AttendanceRule> wrapper = new EntityWrapper<>();
        wrapper.eq("rule_code",ruleCode);
        wrapper.eq("del_status",0);
        AttendanceRule attendanceRule = attendanceRuleMapper.selectById(wrapper);
        if (attendanceRule == null ) {
            return null;
        }
        return AttendanceRuleAssembler.INSTANCE.entityToVO(attendanceRule);
    }
}
